package eerbaar.client.internal.ctr.pet;

import eerbaar.client.ctr.IPersonaCmd;
import eerbaar.client.exp.IExp;
import eerbaar.client.exp.IScope;
import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.WrongPositionException;

public class PersonExpression implements IExp {

    private final IPersonaCmd cmd;
    private final IPersona persona;
    private final IGame game;

    public PersonExpression(IGame game, IPersona persona, IPersonaCmd cmd) {
        this.persona = persona;
        this.cmd = cmd;
        this.game = game;
    }

    @Override
    public Object calcDestination(IScope scope) {

        try {
            return this.cmd.calcDestination(this.game, this.persona);
        } catch (WrongPositionException e) {
            //
        }

        return this.persona.getPosition();
    }

    @Override
    public IScope interpret(IScope scope) {

        try {

            boolean finished = this.cmd.execute(this.game, this.persona);

            scope.getState().setFinished(finished);

        } catch (WrongPositionException e) {

            scope.getState().setFinished(true);
        }

        return scope;
    }

    @Override
    public boolean isInteresting(String elementName) {
        return false;
    }

    @Override
    public void populateValueArea(IScope scope) {
        //
    }

    @Override
    public String toString() {

        return "" + this.persona.getName() + "(" + this.cmd + ")";
    }

    @Override
    public void updateArea(IScope scope) {
        //
    }

}
